package com.syezon.wifikey.splash;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.web.WebHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AgreementDialog extends Dialog {

    //private static final String AGREEMENT_URL = "http://res.ipingke.com/wifikey/agreement.html";
    private static final String AGREEMENT_URL = "http://res.ipingke.com/wifikey/foxors/agreement.html";
    private static final String LOCAL_AGREEMENT_URL = "file:///android_asset/license/agreement.html";
    //private static final String PRIVACY_URL = "http://res.ipingke.com/wifikey/privacy.html";
    private static final String PRIVACY_URL = "http://res.ipingke.com/wifikey/foxors/privacy.html";
    private static final String LOCAL_PRIVACY_URL = "file:///android_asset/license/privacy.html";

    @BindView(R.id.tv_agreement)
    TextView mTvAgreement;

    private AgreementCallBack mCallBack;

    public void setCallBack(AgreementCallBack callBack) {
        this.mCallBack = callBack;
    }

    public static void showAgreement(Activity activity, AgreementCallBack callBack) {
        AgreementDialog agreementDialog = new AgreementDialog(activity);
        agreementDialog.setCallBack(callBack);
        agreementDialog.setOwnerActivity(activity);
        agreementDialog.show();
    }

    public AgreementDialog(@NonNull Context context) {
        super(context);
    }

    public AgreementDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.app_splash_dialog_agreement);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        initView();
    }

    private void initView() {
        mTvAgreement.setHighlightColor(Color.TRANSPARENT);
        SpanUtils.with(mTvAgreement)
                .append("在您使用前请务必审阅")
                .append("《用户协议》")
                .setForegroundColor(Color.parseColor("#4596F8"))
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Helper.showAgreement(getContext());
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(Color.parseColor("#4596F8"));
                        ds.setUnderlineText(false);
                    }
                })
                .append("及")
                .append("《隐私政策》")
                .setForegroundColor(Color.parseColor("#4596F8"))
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Helper.showPrivacy(getContext());
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(Color.parseColor("#4596F8"));
                        ds.setUnderlineText(false);
                    }
                })
                .append("内的所有条款，我们会遵循隐私政策收集、使用信息。")
                .create();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Activity ownerActivity = getOwnerActivity();
            if (ownerActivity != null) {
                ownerActivity.finish();
            }
            return true;
        } else {
            return true;
        }
    }

    @OnClick({R.id.tv_cancel, R.id.tv_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                if (mCallBack != null) {
                    mCallBack.onDelay();
                }
                break;
            case R.id.tv_agree:
                if (mCallBack != null) {
                    mCallBack.onAgree();
                    dismiss();
                }
                break;
        }
    }

    public interface AgreementCallBack {
        void onAgree();

        void onDelay();
    }

    public static class Helper {
        public static void showAgreement(Context context) {
            boolean connected = NetworkUtils.isConnected();
            String title = "用户协议";
            if (connected) {
                WebHelper.showAd(context, AGREEMENT_URL, title);
            } else {
                WebHelper.showAd(context, LOCAL_AGREEMENT_URL, title);
            }
        }

        public static void showPrivacy(Context context) {
            boolean connected = NetworkUtils.isConnected();
            String title = "隐私政策";
            if (connected) {
                WebHelper.showAd(context, PRIVACY_URL, title);
            } else {
                WebHelper.showAd(context, LOCAL_PRIVACY_URL, title);
            }
        }
    }
}
